/**
 * 
 */
package edu.byu.nlp.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset;

/**
 * @author rah67
 *
 */
public class Files2 {

	private Files2() {}
	
	private static Iterable<String> open(Reader rdr) {
		return new LineReaderIterable(new BufferedReader(rdr));
	}
	
	public static Iterable<String> open(File file) throws FileNotFoundException {
		return open(new FileReader(file));
	}
	
	public static Iterable<String> open(String filename) throws FileNotFoundException {
		return open(new FileReader(filename));
	}

	public static Iterable<String> open(String filename, Charset charset) throws FileNotFoundException {
		return open(new InputStreamReader(new FileInputStream(filename), charset));
	}

	public static Iterable<String> open(File file, Charset charset) throws FileNotFoundException {
		return open(new InputStreamReader(new FileInputStream(file), charset));
	}

	public static Iterable<String> open(Class<?> clz, String resource) {
		return open(new InputStreamReader(clz.getResourceAsStream(resource)));
	}
	
	public static void writeLines(Iterable<? extends CharSequence> it, String fn)
			throws IOException {
		writeLines(it, new File(fn));
	}
	
	public static void writeLines(Iterable<? extends CharSequence> it, File file) 
			throws IOException {
		Writers.writeLines(new FileWriter(file), it);
	}
	
	public static void writeLines(Iterable<? extends CharSequence> it, String fn, Charset charset)
			throws IOException {
		writeLines(it, new File(fn), charset);
	}
	
	public static void writeLines(Iterable<? extends CharSequence> it, File file, Charset charset) 
			throws FileNotFoundException {
		Writers.writeLines(new OutputStreamWriter(new FileOutputStream(file), charset), it);
	}
	
}
